package com.hnkywl.sts.dao.sys;

import com.hnkywl.sts.entity.sys.Gw;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Melody_for_love
 * Date: 13-7-15
 * Time: 下午3:20
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class GwpzDao extends SimpleHibernateTemplate<Gw, Long> {
    public Page<Gw> findGsjgGwList(Long gsjgId,int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        Page<Gw> page = new Page<Gw>(pageNo, pageSize, 0);
        //==============记录数量===============
        if (gsjgId != null) {
            hql = "select count(*) from Gw g join g.gsjgList gs where gs.id="+gsjgId;
        } else{
            hql = "select count(*) from Gw where 1=1";
        }
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        if (gsjgId != null) {
            hql = "select g from Gw g join g.gsjgList gs where gs.id="+gsjgId;
        } else{
            hql = "from Gw where 1=1";
        }
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    public Page<Gw> findGwSearchList(int pageNo, int pageSize) {
        Page<Gw> page = new Page<Gw>();
        page.setAutoCount(true);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setOrder("desc");
        page.setOrderBy("lastUpdated");
        return this.findAll(page);
    }

}
